/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.beans.account;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Tian
 */
@Stateless
public class AccountEntityFacade implements AccountEntityFacadeRemote {

    @PersistenceContext
    private EntityManager em;

    public List<AccountEntity> getAllAccounts() {

        Query temp;

        try {

            temp = em.createNamedQuery("findAllAccounts");


        } catch (Exception e) {

            temp = null;
        }

        return (List<AccountEntity>) temp.getResultList();
    }

     public List<AccountEntity> getAllAccountsByType(String type){
         
        List<AccountEntity> temp;

        try {

            temp = em.createNamedQuery("findAllAccountsByType").setParameter("accountType", type).getResultList();

            AccountEntity test = getAccountByAccountIBAN(temp.get(0).getAccountIBAN());
            
            if(test == null)
                temp = null;
            
        } catch (Exception e) {

            temp = null;
        }

        return (List<AccountEntity>) temp;
         
     }
    
    
    
    public AccountEntity getAccountById(Long id) {

        AccountEntity account = null;

        try {

            account = (AccountEntity) em.createNamedQuery("findAccountById").setParameter("id", id).getSingleResult();

        } catch (Exception e) {

            account = null;

        }

        return account;
    }

    public AccountEntity getAccountByAccountIBAN(String iban) {

        AccountEntity account = null;

        try {

            account = (AccountEntity) em.createNamedQuery("findAccountByIBAN").setParameter("accountIBAN", iban).getSingleResult();

        } catch (Exception e) {

            account = null;
        }

        return account;
    }

    public void deposit(String iban, double amount) {

        AccountEntity account = getAccountByAccountIBAN(iban);

        if (account != null) {

            account.deposit(amount);
            em.merge(account);
        }
    }

    public double getBalance(String iban) {

        double balance;
        AccountEntity account = getAccountByAccountIBAN(iban);

        if (account == null) {

            balance = Double.NEGATIVE_INFINITY;
        } else {

            balance = account.getAccountBalance();

        }

        return balance;
    }

    public boolean withdraw(double amount, String iban) {
        
        AccountEntity account = getAccountByAccountIBAN(iban);

        if (account == null) {

            return false;
            
        } 
        else {

            if(account.withdraw(amount)){
                
                em.merge(account);
                return true;
            }
            else
                return false;
        }
    }

    public AccountEntity addAccount(String iban,int type) {

        AccountEntity account = new AccountEntity();
        account.setAccountIBAN(iban);
        account.setAccountType(type);
        em.persist(account);
        return account;
    // or return id not sure yet
    }
    
    /**
     * 
     * @author Houssem Z.
     */
    public void persistAccount(AccountEntity account) {

        em.persist(account);
        
    }

    public AccountEntity removeAccount(Long id) {

        AccountEntity account = (AccountEntity) em.createNamedQuery("findAccountById").getSingleResult();

        if (account != null) {

            em.remove(account);

        }

        return account;
        // not sure yet
    }

    public List<AccountEntity> removeAllAccounts() {

        List<AccountEntity> list;

        try {

            list = getAllAccounts();

            if (list != null && list.size()>0) {

                for (int i = 0; i < list.size(); i++) {

                    em.remove(list.get(i));
                }
            }
            else
                list = null;
            
        } catch (Exception e) {

            list = null;
        }

        return list;
        // not sure yet
    }
    
    public AccountEntity removeAccountByIBAN(String iban) {

        AccountEntity account = this.getAccountByAccountIBAN(iban);

        if (account != null) {

            em.remove(account);

        }

        return account;
        // not sure yet
    }
    
}
